fldmeta_prop.func.SubProcess = {};

//config sub process variables
fldmeta_prop.func.configSubProcessVars = function(rId,cId) {
	var w1 = fm_createCenterWindow('wid_subprocessvars', fmd_i18n_title_configvar, 600, 600);
	var count = 1;
	
	var lo1 = w1.attachLayout("2E");
	lo1.cells('a').hideHeader();
	lo1.cells('b').hideHeader();
	lo1.cells('b').setHeight(50);
	
	var tb1 = lo1.cells('a').attachToolbar();
	tb1.setIconsPath(flvar.imagepath);
	tb1.addButton('btn_add', 0, fmd_i18n_b_newl, 'new1.png', 'new1_dis.png');
	tb1.addButton('btn_del', 1, fmd_i18n_b_del, 'delete1.png', 'delete1_dis.png');
	
	var str2 = [
		   		{ type:"block" , name:"form_block_1", offsetLeft:(dhx_skin=="dhx_web"?"235":"240"), offsetTop:"0", list:[
		   		{ type:"button" , name:"fb_ok", value:fmd_i18n_b_ok, inputTop:0  }
		   		]  }
		   	];
	var fm2 = lo1.cells('b').attachForm(str2);
	
	//grid
	var ragrid = lo1.cells('a').attachGrid();
	ragrid.setHeader([fmd_i18n_prop_variablename,
	                  fmd_i18n_prop_type,
	                  fmd_i18n_prop_allowin,
	                  fmd_i18n_prop_allowout,
	                  	fmd_i18n_prop_desc],
	                    null,
	                    ["text-align:center;",
	                     "text-align:center;",
	                     "text-align:center;",
	                     "text-align:center;",
	                     "text-align:center;"]);
	ragrid.setInitWidths("100,100,60,60,*");
	ragrid.setColumnIds("VARNAME,VARTYPE,ALLOWIN,ALLOWOUT,VARDESC");
	ragrid.setColTypes("ed,coro,ch,ch,ed");
	ragrid.setColSorting("str,str,int,int,na");
	ragrid.setColAlign("left,left,center,center,left");
	//ragrid.setEditable(true);
	ragrid.setLang(flvar.lang);
	ragrid.enableValidation(true,true);
	ragrid.enableEditEvents(true, false, false);
	ragrid.setColValidators("ValidVariable,NotEmpty,,,NotEmpty");
	ragrid.init();
	
	tb1.attachEvent("onClick", function(tid) {
		if (tid=='btn_add') {
			var rid = count++;
			ragrid.addRow(rid, [null, 'string', 1, 1, null]);
			ragrid.grid_bindCombo(rid, 1, 
					fmd_i18n_prop_processvar_vals, 
					{"comboSize":10, "comboWidth":100}, 
					'string');
		} else if (tid=='btn_del') {
			var rn = ragrid.getSelectedRowId();
			if (!rn) {
				dhtmlx.message(fmd_i18n_prop_msg_plsselline);
				return;
			} else {
				ragrid.deleteRow(rn);
			}
		}
	});
	
	//load existing
	if (fl.curflow.bpmnConfig[fl.curComp.id]["processvars"]) {
		for (id in fl.curflow.bpmnConfig[fl.curComp.id]["processvars"]) {
			var rid = count++;
			ragrid.addRow(rid, [id, fl.curflow.bpmnConfig[fl.curComp.id]["processvars"][id].type,
			                    fl.curflow.bpmnConfig[fl.curComp.id]["processvars"][id].allowin, 
			                    fl.curflow.bpmnConfig[fl.curComp.id]["processvars"][id].allowout, 
			                    fl.curflow.bpmnConfig[fl.curComp.id]["processvars"][id].desc]);
			ragrid.grid_bindCombo(rid, 1, 
					fmd_i18n_prop_processvar_vals, 
					{"comboSize":10, "comboWidth":100}, 
					fl.curflow.bpmnConfig[fl.curComp.id]["processvars"][id].type);
		}
	}
	
	fm2.attachEvent("onButtonClick", function(btid) {
		if (btid=='fb_ok') {
			var s = {};
			var vars = {};
			//validate values
			var rn = ragrid.getRowsNum();
			if (rn) {
				for (var i=0; i<rn;i++) {
					var rid = ragrid.getRowId(i);
					var id = ragrid.cells(rid,0).getValue();
					if((ragrid.cells(rid,0).getAttribute("validate") && !ragrid.validateCell(rid,0))
							|| (ragrid.cells(rid,4).getAttribute("validate") && !ragrid.validateCell(rid,4))) {
						msg(fmd_i18n_msg_plscorrect);
						return;
					} else if (s[id]) {
						msg(fmd_i18n_msg_plsdeldup + ' : ' + id);
						return;
					} else {
						s[id] = true;
					}
				};
				//get values
				ragrid.forEachRow(function(id){
					vars[ragrid.cells(id,0).getValue()] = {
						"type" : ragrid.cells(id,1).getValue(),
						"allowin" : ragrid.cells(id,2).getValue(),
						"allowout" : ragrid.cells(id,3).getValue(),
						"desc" : ragrid.cells(id,4).getValue()
					};
				});
			}
			//fl.curflow.bpmnConfig[fl.curComp.id]["processvars"] = vars;
			fmdc.grid_prop.cells(rId, cId).setValue(vars, true);	//第二个参数必须为true，触发事件
			//console.debug("process variables="+JSON.stringify(fl.curflow.bpmnConfig[fl.curComp.id]["processvars"]));
			w1.close();
		}
	});
};

fldmeta_prop.func.SubProcess.onDisplayStyle = function(elemid, rId, nv) {
	if (rId=="displayStyle_bgColor") {
		Snap("g[data-element-id='"+elemid+"'] g.djs-visual rect").attr({"fill" : nv || "#fafdde"});
	} else if (rId=="displayStyle_bgOpacity") {
		Snap("g[data-element-id='"+elemid+"'] g.djs-visual rect").attr({ opacity: Number(nv) || 0.8});
	}
};

//properties settings for SubProcess
fldmeta_prop.control.SubProcess = {
		"includes-properties" : {
			"common" : fldmeta_prop.common.all.properties,
			"activitycommon" : fldmeta_prop.common.activity.properties
		},
		"properties" : {
			/*"processvars" : {
    			"name" : fmd_i18n_prop_subprocessvars,
    			"cellType" : {
    				"type" : "btnex",
    				"config" : {
	    				"onclick" : fldmeta_prop.func.configSubProcessVars
	    			}
    			},
		    	"img" : "processvars.png",
		    	"value" : {"default" : {}},
		    	"validatorFunc" : function(elemid, val, prefix){
		    		return null;
		    	}
    		},
    		"route" : fldmeta_prop.common.predefined["route"],
		    "duration" : fldmeta_prop.common.predefined["duration"],
		    "manualRemind" : fldmeta_prop.common.predefined["manualRemind"],
		    "remind" : fldmeta_prop.common.predefined["remind"]*/
		},
		"abandon-properties" : fldmeta_prop.func.getAbandonPropertiesForConditional,
		"styles" : {
			"displayStyle" : {
				"name" : fmd_i18n_prop_tip_customstyle,
				"img" : "css.png",
		    	"cellType" : {
		    		"type" : "coro",
		    		"options" : fmd_i18n_prop_displayStyle_vals
		    	},
		    	"value" : {"default":"theme"},
		    	"conditional-sub" : {
		    		"custom" : {
		    			"displayStyle_bgColor" : function() {
		    				var obj1 = MyUtilJs.copyObj(fldmeta_prop.common.predefined["displayStyle_bgColor"]);
		    				obj1.value["default"] = flstyle.elem.attrs.subprocess.fill;
		    				return obj1;
		    			},
		        		"displayStyle_bgOpacity" : function() {
		    				var obj1 = MyUtilJs.copyObj(fldmeta_prop.common.predefined["displayStyle_bgOpacity"]);
		    				obj1.value["default"] = flstyle.elem.attrs.subprocess.opacity;
		    				return obj1;
		    			}/*,
		        		"displayStyle_BorderStrokeWidth" : fldmeta_prop.common.predefined["displayStyle_BorderStrokeWidth"],
		        		"displayStyle_BorderColor" : fldmeta_prop.common.predefined["displayStyle_BorderColor"],
		        		"displayStyle_BorderEffect" : fldmeta_prop.common.predefined["displayStyle_BorderEffect"]*/
		    		}
		    	}
			}
		},
		"onApply" : function() {
		},
		"propDhxGridEvents" : {
			"onCellChanged" : function(rId,cId,nv) {
				fldmeta_prop.func.onCellChanged_remind(rId,cId,nv);
				fldmeta_prop.func.onCellChanged_stylecommon(rId,cId,nv);
			}
		}
	};


